package com.lamer.console.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lamer.console.base.entity.Menu;
import com.lamer.console.base.mapper.MenuMapper;
import com.lamer.console.base.service.IBaseService;
import com.lamer.console.base.service.IMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 黄诗鹏
 * @since 2021-09-25
 */
@Service("MenuServiceImpl")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService, IBaseService {

    @Resource
    private MenuMapper menuMapper;

    @Override
    public List<Menu> treeList() {
        List<Menu> list = menuMapper.selectList(null);
        return compositeMenuList(list);
    }

    @Override
    public Page<Menu> treePage(String searchKey, Page<Menu> page) {
        Page<Menu> tree = getIsParentPage(searchKey, page);
        List<Menu> subList = getSubListByIds(tree.getRecords());
        return compositeMenuTree(tree, subList);
    }
    private Page<Menu> getIsParentPage(String searchKey, Page<Menu> page){
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getParentId, 0)
                    .like(Menu::getMenuName, searchKey);
        return menuMapper.selectPage(page, queryWrapper);
    }
    private List<Menu> getSubListByIds(List<Menu> list){
        List<Integer> treeIds = list.stream().map(Menu::getId).collect(Collectors.toList());
        LambdaQueryWrapper<Menu> subQueryWrapper = new LambdaQueryWrapper<>();
        subQueryWrapper.in(Menu::getParentId, treeIds);
        return menuMapper.selectList(subQueryWrapper);
    }
    private Page<Menu> compositeMenuTree(Page<Menu> tree, List<Menu> subList){
        tree.getRecords().forEach( parent -> subList.stream()
                        .filter(son -> parent.getId().equals(son.getParentId()))
                        .forEach(parent::add));
        return tree;
    }

    @Override
    public List<Menu> getByUserId(int id) {
        List<Menu> menus = menuMapper.selectByUserId(id);
        return compositeMenuList(menus);
    }
    private List<Menu> compositeMenuList(List<Menu> list){
        List<Menu> result = new ArrayList<>();
        list.stream().filter(menu -> menu.getParentId() == 0)
                    .forEach(menu -> {
                        list.stream().filter(subMenu -> menu.getId().equals(subMenu.getParentId()))
                                    .forEach(menu::add);
                        result.add(menu);
                    });
        return result;
    }

    @Override
    public boolean updateStatusById(int id) {
        Menu menu = menuMapper.selectById(id);
        int status = menu.getStatus() == 1 ? 0 : 1;
        menu.setStatus(status);
        int update = menuMapper.updateById(menu);
        return update >= 1;
    }
}
